# -*- coding:utf-8 -*-
"""
Created on 11 sep 2011

@author: Olof Manbo
"""

import httplib
import urllib
import test_pb2

import requests
import random

header = {"Content-Type": "application/x-www-form-urlencoded"}

#conn = httplib.HTTPConnection("sveanas.appspot.com")
conn = httplib.HTTPConnection("localhost:8888")

def sendProcessRequest(type, argument):
    processRequest = test_pb2.ProcessRequest()
    processRequest.command = type
    processRequest.argument = argument
    
    
    
    params = processRequest.SerializeToString()
    conn.request("POST", "/simulatorwebservice/api/proto/process", params , header)
    r1 = conn.getresponse()
    print r1.status, r1.reason
    data1 = r1.read()
    conn.close()
    print data1
    
    processResponse = test_pb2.ProcessResponse()
    
    processResponse.ParseFromString(data1)
    print processResponse.success

    

#print "NEW"
#sendProcessRequest(test_pb2.ProcessRequest.NEW, "New")


#resourceRequest = test_pb2.ResourceRequest()
#
#mResource1 = resourceRequest.put_resource.add()
#mResource1.key = 1
#weight1 = mResource1.weight.add()
#weight1.group = 0
##weight1.value = -1.5
#weight1.value = random.random()*2-1
#weight1.resource_reference = 1
#
#mResource2 = resourceRequest.put_resource.add()
#mResource2.key = 2
#weight2 = mResource1.weight.add()
#weight2.group = 0
##weight2.value = 1.5
#weight2.value = random.random()*2-1
#weight2.resource_reference = 1
#
#
#
#params = resourceRequest.SerializeToString() 
#
#
#conn.request("POST", "/simulatorwebservice/api/proto/resource", params , header)
#
#r1 = conn.getresponse()
#print "/simulatorwebservice/api/proto/resource"
#print r1.status, r1.reason
#data1 = r1.read()
#conn.close()
#print data1
#
#resourceResponse = test_pb2.ResourceResponse()
#
#resourceResponse.ParseFromString(data1)
#print "resourceResponse.success"
#print resourceResponse.success

#nodeRequest = test_pb2.NodeRequest()
#
#for n in range(20):
#    mNode1 = nodeRequest.putNodes.add()
#    mNode1.resource_key = n%2+1
#    mNode1.x = random.randint(-100,100)
#    mNode1.y = random.randint(-100,100)
##mNode2 = nodeRequest.putNodes.add()
##mNode2.resource_key = 2
##mNode2.x = -100
##mNode2.y = 100
#
#params = nodeRequest.SerializeToString() 
#
#
#conn.request("POST", "/simulatorwebservice/api/proto/node", params , header)
#
#r1 = conn.getresponse()
#print r1.status, r1.reason
#data1 = r1.read()
#conn.close()
#print "data1"
#print data1
#
#nodeResponse = test_pb2.NodeResponse()
#
#nodeResponse.ParseFromString(data1)
#print "test_pb2.NodeResponse() %s"%nodeResponse.success



#sendProcessRequest(test_pb2.ProcessRequest.REFRESH, "Refresh")

requests.sendNodeRequest("localhost:8888")

#for n in range(10):
#    sendProcessRequest(test_pb2.ProcessRequest.RUN, "Run")
#    requests.sendNodeRequest()

    
conn.close()